﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;

namespace GreatB2C.Config
{
    public class BaseConfig
    {
        internal enum eConfigs
        {
            /// <summary>
            /// 网站信息配置
            /// </summary>
            GreatB2C ,
            /// <summary>
            /// 文件上传配置
            /// </summary>
            UploadFile ,
            /// <summary>
            /// 用户参数配置
            /// </summary>
            UserParams ,
            /// <summary>
            /// 用户参数配置
            /// </summary>
            Email ,
            /// <summary>
            /// 缩略图
            /// </summary>
            Thumbnail ,
            /// <summary>
            /// IP段限制
            /// </summary>
            IPLimit ,
            /// <summary>
            /// 手机短信配置
            /// </summary>
            SMS ,
            /// <summary>
            /// RSS配置
            /// </summary>
            RSS ,
            /// <summary>
            /// 商店参数
            /// </summary>
            Shop ,
            /// <summary>
            /// 商店模板
            /// </summary>
            Template
        }

        /// <summary>
        /// 配置文件列表
        /// </summary>
        internal static string[] ConfigList = new string[]
            {
                "~/Config/Sytem/SiteInfo/GreatB2C.config" ,
                "~/Config/Sytem/SiteInfo/UploadFile.config" ,
                "~/Config/Sytem/SiteInfo/UserParams.config" ,
                "~/Config/Sytem/SiteInfo/Email.config" ,
                "~/Config/Sytem/SiteInfo/Thumbnail.config" ,
                "~/Config/Sytem/SiteInfo/IPLimit.config" ,
                "~/Config/Sytem/SiteInfo/SMS.config" ,
                "~/Config/Sytem/SiteInfo/RSS.config" ,
                "~/Config/Sytem/Shop/Shop.config" ,
                "~/Config/Sytem/Shop/Template.config"
            };

        /// <summary>
        /// 读GreatB2C.config取配置文件
        /// </summary>
        /// <param name="whichConfig">指定配置文件</param>
        /// <param name="target">指定节点名称</param>
        /// <returns></returns>
        static internal string GetConfigValue( eConfigs whichConfig , string target )
        {
            string configpath = ConfigList[ ( int )whichConfig ].ToString( );
            string path = HttpContext.Current.Server.MapPath( configpath );
            return GetConfigValue( target , path );
        }

        /// <summary>
        /// 读GreatB2C.config取配置文件
        /// </summary>
        /// <param name="target">指定节点名称</param>
        /// <param name="xmlPath">配置文件路径</param>
        /// <returns></returns>
        static internal string GetConfigValue( string target , string xmlPath )
        {
            System.Xml.XmlDocument xdoc = new XmlDocument( );
            xdoc.Load( xmlPath );
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName( target );
            return elemList[ 0 ].InnerXml;
        }

        static internal bool SetConfigValue( eConfigs whichConfig , string Key , string Value )
        {
            string configpath = ConfigList[ ( int )whichConfig ].ToString( );
            XmlDocument xmlDoc = new XmlDocument( );
            string xmlPath = HttpContext.Current.Server.MapPath( configpath );

            if ( System.IO.File.Exists( xmlPath ) )
            {
                xmlDoc.Load( xmlPath );
                XmlElement root = xmlDoc.DocumentElement;
                XmlNodeList elemList = root.GetElementsByTagName( Key );
                elemList[ 0 ].InnerXml = Value.Trim( );
                xmlDoc.Save( xmlPath );

                return true;
            }

            return false;
        }

        static internal bool SetConfigForBatch( eConfigs whichConfig , Dictionary<string , string> Items )
        {
            string configpath = ConfigList[ ( int )whichConfig ].ToString( );
            XmlDocument xmlDoc = new XmlDocument( );
            string xmlPath = HttpContext.Current.Server.MapPath( configpath );

            if ( System.IO.File.Exists( xmlPath ) )
            {
                xmlDoc.Load( xmlPath );
                XmlElement root = xmlDoc.DocumentElement;

                foreach ( KeyValuePair<string,string> de in Items )
                {
                    XmlNodeList elemList = root.GetElementsByTagName( de.Key );
                    elemList[ 0 ].InnerXml = de.Value.ToString( ).Trim( );
                }

                xmlDoc.Save( xmlPath );

                return true;
            }

            return false;
        }
    }
}
